package cn.edgar.org.controller;

import cn.edgar.basic.utils.AjaxResult;
import cn.edgar.basic.utils.PageList;
import cn.edgar.org.domain.Department;
import cn.edgar.org.query.DepartmentQuery;
import cn.edgar.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
/**
* @Description: TODO
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/24 11:54
*/
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    /* 
    * @Title: loadById
    * @Description: TODO
    * @Author: My.Yang
    * @Version: 1.0
    * @Date:  2022/6/24 11:52  
    * @Parameters: [id]
    * @Return cn.edgar.org.domain.Department
    */
    public Department loadById (@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }



    @GetMapping// 请求地址：http://localhost:8080/department GET请求
    /*
    * @Title: loadAll
    * @Description: TODO
    * @Author: My.Yang
    * @Version: 1.0
    * @Date:  2022/6/24 20:00
    * @Parameters: []
    * @Return java.util.List<cn.edgar.org.domain.Department>
    */
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }


    @DeleteMapping("/{id}") 
    /* 
    * @Title: remove
    * @Description: TODO
    * @Author: My.Yang
    * @Version: 1.0
    * @Date:  2022/6/24 20:02  
    * @Parameters: [id]
    * @Return cn.edgar.basic.utils.AjaxResult
    */
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PutMapping
    /* 
    * @Title: addOrUpt
    * @Description: TODO
    * @Author: My.Yang
    * @Version: 1.0
    * @Date:  2022/6/24 20:02  
    * @Parameters: [department]
    * @Return cn.edgar.basic.utils.AjaxResult
    */
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    /* 
    * @Title: queryData
    * @Description: TODO
    * @Author: My.Yang
    * @Version: 1.0
    * @Date:  2022/6/24 20:03
    * @Parameters: [departmentQuery]
    * @Return cn.edgar.basic.utils.PageList<cn.edgar.org.domain.Department>
    */
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;
    }


    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @GetMapping("/tree")
    public List<Department> tree(){
        return departmentService.tree();
    }

}
